<?php
function callOpenAPI($url, $parameters, $apiKey) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($parameters));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Content-Type: application/json",
        "Authorization: Bearer " . $apiKey
    ));
    $response = curl_exec($ch);
    curl_close($ch);
    return json_decode($response, true);
}

// 调用DAVINCI API接口
$response = callOpenAPI(
    "https://api.openai.com/v1/engines/davinci/completions",
    array(
        "prompt" => "Hello, I'm a PHP developer. Can you please tell me more about OpenAI?",
        "max_tokens" => 50,
        "temperature" => 0.5,
        "stop" => ["\n"]
    ),
    "YOUR_API_KEY"
);

// 打印API响应结果
print_r($response);
